<?php
/***************************************************************************
 * @Description:
 * 
 * @FileName:         Content.php
 * @Author :          liyong5@staff.sina.com.cn
 * @CreatedTime:      Thu 31 May 2018 11:22:50 AM CST
 ************************************************************************/

namespace app\modules\core\models;

use yii\base\InvalidArgumentException;
use yii\mongodb\ActiveRecord;

class Content extends ActiveRecord
{
    private $baseAttrs = ['_id', 'id', 'model', 'title', 'author', 'ctime'];


    private $attributes = [];

    private $lables;

    private $rules = [
        [['id', 'model', 'title'], 'required']
    ];

    public static function collectionName()
    {
        return 'content';
    }

    /**
     * 内容属性由所关联的模型定义
     * 如果内容未关联模型，或者未设置属性则抛出异常
     */
    public function attributes() {
        return $this->attributes;
    }

    public function attributeLabels() {
        return $this->lables;
    }

    public function rules() {
        return $this->rules;
    }

    public static function primaryKey() {
        return ['id'];
    }

    public function setAttrLable($lables) {
        $this->lables = $lables;
    }

    public function setRules($rules) {
        return $this->rules = array_merge($this->rules, $rules);
    }

    public function setAttrs($attrs) {
        if (is_array($attrs) && count($attrs)) {
            return $this->attributes = array_merge($this->baseAttrs, $attrs);
        }
        throw new InvalidArgumentException('error');
    }

    public function getAttrs() {
        return $this->attributes;
    }

    public function genId() {
        $codeTable = "ABCDEFJHIJKLMNOPQRstuvwxyzabcdefjhijklmnopqrSTUVWXYZ0123456789";

        $yearCode = $codeTable[intval(date('y'))];
        $monthCode = $codeTable[intval(date('m'))];
        $dayCode = $codeTable[intval(date('d'))];
        $hourCode = $codeTable[intval(date('H'))];
        $minuteCode = $codeTable[intval(date('i'))];
        $secodeCode = $codeTable[intval(date('s'))];

        $microSecod = substr(microtime(), 2, 4);
        return $yearCode . $monthCode . $dayCode . $hourCode . $minuteCode . $secodeCode . $microSecod;
    }
}
